package com.example.demo.service.impl

import com.example.demo.config.Constant
import com.example.demo.service.UploadService
import com.example.demo.vo.UploaFileVo
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.io.File


@Service
class UploadServiceImpl: UploadService {

    override fun uploadImage(file: MultipartFile): UploaFileVo? {
        val location = System.getProperty("user.dir") + "/Resource/images/"
        val locationFile = File(location)
        if (!locationFile.exists()) locationFile.mkdirs()
        val filePath = locationFile.path
        val fileName = file.originalFilename
        val dest = File("$filePath/$fileName")
        file.transferTo(dest)

        val uploaFileVo = UploaFileVo()
        uploaFileVo.url = Constant.RESOURCE_BASEURL + "/images/$fileName"
        return uploaFileVo
    }

    override fun uploadVideo(file: MultipartFile): UploaFileVo? {
        val location = System.getProperty("user.dir") + "/Resource/video/"
        val locationFile = File(location)
        if (!locationFile.exists()) locationFile.mkdirs()
        val filePath = locationFile.path
        val fileName = file.originalFilename
        val dest = File("$filePath/$fileName")
        file.transferTo(dest)

        val uploaFileVo = UploaFileVo()
        uploaFileVo.url = Constant.RESOURCE_BASEURL + "/video/$fileName"
        return uploaFileVo
    }

}